<?php
class WordsController extends AppController {

	var $name = 'Words';
	var $components = array('RequestHandler');
	var $uses = array('Language','Word','Activity');

	function index() {
		$this->paginage=array(
			'limit'=>30,
			'order'=>'Word.id desc'
				);
		if (isset($this->uid) and isset($this->user)) {
			$words = $this->paginate('Word',array("Word.user_id"=>$this->uid));
		}else{
			$words = $this->paginate('Word');
		}
		$this->set('words', $words);
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid word', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('word', $this->Word->read(null, $id));
	}

    function dictate()
    {
       if (isset($this->params['url']['uid'])) {
           $user_id =  $this->params['url']['uid'];
           $user = $this->User->findById($user_id);
            $words = $this->paginate('Word',array('Word.user_id'=>$user_id));
        } else{
            $words = array();
        }
        $this->set('word', $words);
        $this->set('user', $user);
    }
    function ajax_dictate(){
        
    }
	function add() {

		if (!empty($this->data)) {
			$this->Word->create();
			#handle ajax requests.....
  		if ($this->RequestHandler->isAjax()){
			if ($this->Word->save($this->data)){ 
				$this->Activity->create();
				$activate = array(
					'Activity'=>array(
						'user_id'=>$this->cuid,
						'body'=>"添加了新<a href='/people/{$this->cuuid}/words'>单词</a>。",
						'newsType'=>'Word'
						)
					);
				$this->Activity->save($activate);
				$this->autoRender = false;
				$word = array();
				$word['word']=$this->data['Word']['word'];
				$word['description']=$this->data['Word']['description'];
				$language = $this->Language->findById($this->data['Word']['language_id']);
				$language = $language['Language']['name'];
				$word['language']=$language;
				$json = json_encode($word);
				echo $json;
              } 
              exit; 
      }
			if ($this->Word->save($this->data)) {
				$this->Activity->create();
				$activate = array(
					'Activity'=>array(
						'user_id'=>$this->cuid,
						'body'=>"添加了新<a href='/people/{$this->cuuid}/words'>单词</a>。",
						'newsType'=>'Word'
						)
					);
				$this->Activity->save($activate);
				$this->Session->setFlash(__('The word has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The word could not be saved. Please, try again.', true));
			}
		}
		$languages = $this->Word->Language->find('list');
		$this->set(compact('languages'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid word', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Word->save($this->data)) {
				$this->Session->setFlash(__('The word has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The word could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Word->read(null, $id);
		}
		$users = $this->Word->User->find('list');
		$languages = $this->Word->Language->find('list');
		$this->set(compact('users', 'languages'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for word', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Word->delete($id)) {
			$this->Session->setFlash(__('Word deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Word was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>